package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.CibB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CibConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cib.ebank.CibMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * 兴业银行B2C网关支付
 * 
 * @CibB2cBankServiceImpl.java
 * @author zhengxue
 * @2014-11-20 上午10:19:56 www.gopay.com.cn Inc.All rights reserved.
 */
public class CibB2cBankServiceImpl implements BankInteractRemoteService {
    @Resource(name = "cibB2cManager")
    private CibB2cManager cibB2cManager;

    /**
     * 支付
     */
    public Result pay(BaseMessage base) {
        Result result = new Result(false);
        try {
            CibMessage messageB2C = new CibMessage();
            BeanUtils.copyProperties(base, messageB2C);
            messageB2C = (CibMessage) cibB2cManager.sign(messageB2C);
            result.setDefaultModel(messageB2C);
            result.setSuccess(true);
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
        }
        return result;
    }

    /**
     * 验签
     */
    public Result bankBack(BaseMessage base) {
        Result result = new Result(false);
        try {
            CibMessage messageB2C = new CibMessage();
            BeanUtils.copyProperties(base, messageB2C);
            boolean flag = (Boolean) cibB2cManager.verify(messageB2C);
            result.setModel("message", messageB2C);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            if (flag) {
                Map<String, String> param = new HashMap<String, String>();
                // 收到银行通知，代表支付成功
                param.put("orderId", messageB2C.getOrderId());
                param.put("amt", messageB2C.getPayAmt());
                param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "00");
                param.put("retCode", "00");
                param.put("retMsg", "验签成功");
                param.put("signData", messageB2C.getSignMessage());
                param.put("isFront", String.valueOf(base.isFront()));
                result.setModel("isNotifyCashier", param);
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
        }
        return result;
    }

    /**
     * 查询
     */
    public Result query(BaseMessage base) {
        Result result = new Result(false);
        try {
            CibMessage messageB2C = new CibMessage();
            BeanUtils.copyProperties(base, messageB2C);
            CibMessage b2c = (CibMessage) cibB2cManager.query(messageB2C);
            result.setDefaultModel(b2c);
            result.setSuccess(true);
            if (BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())) {
                Map<String, String> param = new HashMap<String, String>();
                param.put("orderId", messageB2C.getOrderId());
                param.put("amount", messageB2C.getAmount().toString());
                if (b2c == null) {
                    // 查询订单为空
                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
                    param.put("returnMessage", "银行返回对账结果为空");
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    result.setModel("queryResult", param);
                    return result;
                } else if (!StringUtils.isEmpty(b2c.getCode())) {
                    // 查询订单错误
                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
                    param.put("returnMessage", b2c.getCode() + b2c.getMsg());
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    result.setModel("queryResult", param);
                    return result;
                } else {
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    if (b2c.getOrderStatus() != null && !b2c.getOrderStatus().equals("")) {
                        // 订单状态成功(订单已支付)
                        if (CibConstants.ORDER_STATE_PAYMENT.equals(b2c.getOrderStatus())) {
                            param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
                            param.put("returnMessage", "订单状态 1: 已支付");
                        }
                        // 订单状态成功(订单已结算)
                        else if (CibConstants.ORDER_STATE_SETTLEMENT.equals(b2c.getOrderStatus())) {
                            param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
                            param.put("returnMessage", "订单状态 2: 已结算");
                        }
                        // 订单状态暂不处理(订单待支付)
                        else if (CibConstants.ORDER_STATE_WAITAPAY.equals(b2c.getOrderStatus())) {
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "订单状态 0: 待支付");
                        }
                        // 订单状态暂不处理(订单部分退款)
                        else if (CibConstants.ORDER_STATE_PARTREFUND.equals(b2c.getOrderStatus())) {
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "订单状态 4: 部分退款");
                        }
                        // 订单状态暂不处理(订单全额退款)
                        else if (CibConstants.ORDER_STATE_ALLREFUND.equals(b2c.getOrderStatus())) {
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "订单状态 5: 全额退款");
                        }
                        // 订单状态失败处理(订单已撤销)
                        else if (CibConstants.ORDER_STATE_CANCEL.equals(b2c.getOrderStatus())) {
                            param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
                            param.put("returnMessage", "订单状态 3: 已撤销");
                        }
                        // 订单状态失败处理(订单支付失败）
                        else if (CibConstants.ORDER_STATE_FAIL.equals(b2c.getOrderStatus())) {
                            param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
                            param.put("returnMessage", "订单状态 6: 支付失败");
                        } else {
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "交易结果未明确");
                        }
                    } else {
                        param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                        param.put("returnMessage", "银行返回订单未查到");
                    }
                }
                result.setModel("queryResult", param);
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
        }
        return result;
    }

    @Override
    public Result queryBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refund(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result dayend(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
